import { ProTable, type ProColumns } from '@/components/Pro';
import { envDeviceControllerGetHistoryLiquidCoolData } from '@/services/ess/deviceEnv';
import { DATABASE_TYPE_ENUM, fetchDictInfo } from '@/utils';
import { useIntl, useModel } from '@umijs/max';
import dayjs from 'dayjs';
import React, { useEffect } from 'react';

interface props {
  tabIndex?: string;
  deviceCode?: string;
}

const HistoryData: React.FC<props> = ({ deviceCode }) => {
  const intl = useIntl();
  const { initialState } = useModel('@@initialState');
  let Options_type1: any[] = [];
  let Options_type2: any[] = [];
  let Options_type3: any[] = [];
  let Options_online: any[] = [];
  let Options_faultLeve: any[] = [];
  let Options_runState: any[] = [];
  let Options_runMode: any[] = [];

  const changeFuntion = (key: any, result: any) => {
    const array = ['runState', 'comprState', 'heatState', 'selfLoopState', 'heatState'];
    const array2 = ['enableState'];
    const array3 = ['onOff'];
    const array_onlineState = ['onlineState'];
    const array_faultLeve = ['faultLevel'];
    const array_runMode = ['setRunMode'];
    if (array.includes(key)) {
      if (key === 'runState') {
        return Options_runState.find((res) => res.value === result)?.['label'] || '';
      } else {
        return Options_type1.find((res) => res.value === result)?.['label'] || '';
      }
    } else if (array2.includes(key)) {
      return Options_type2.find((res) => res.value === result)?.['label'] || '';
    } else if (array3.includes(key)) {
      return Options_type3.find((res) => res.value === result)?.['label'] || '';
    } else if (array_onlineState.includes(key)) {
      return Options_online?.find((res) => res.value === result)?.['label'] || '';
    } else if (array_runMode.includes(key)) {
      return Options_runMode?.find((res) => res.value === result)?.['label'] || '';
    } else if (array_faultLeve.includes(key)) {
      return Options_faultLeve?.find((res) => res.value === result)?.['label'] || '';
    } else {
      return result;
    }
  };
  const columns: ProColumns<any>[] = [
    {
      title: intl.formatMessage({ id: 'data.s/N' }),
      key: 'index',
      valueType: 'index',
      fixed: 'left',
      width: 60,
      dataIndex: 'pageIndex',
    },
    {
      title: intl.formatMessage({ id: 'data.acquisitionTime' }),
      dataIndex: 'collectTime',
      key: 'collectTime',
      valueType: 'dateTimeRange',
      fixed: 'left',
      width: 100,
      initialValue: [dayjs().startOf('day'), dayjs()],
      render: (_, record) => {
        return <span>{record.ts}</span>;
      },
    },
    {
      title: intl.formatMessage({ id: 'data.reportingTime' }),
      dataIndex: 'reportTime',
      key: 'reportTime',
      valueType: 'dateTimeRange',
      fixed: 'left',
      width: 100,
      initialValue: [dayjs().startOf('day'), dayjs()],
      render: (_, record) => {
        return <span>{record.reportstTime}</span>;
      },
    },
    {
      title: intl.formatMessage({ id: 'data.deviceModel' }),
      dataIndex: 'product',
      key: 'product',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'data.deviceFactory' }),
      dataIndex: 'producer',
      key: 'producer',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'data.deviceID' }),
      dataIndex: 'liquidCode',
      hideInSearch: true,
      width: 90,
    },
    {
      title: intl.formatMessage({ id: 'data.onlineStatus' }),
      dataIndex: 'onlineState',
      hideInSearch: true,
      width: 100,
      render: (_, record) => {
        return changeFuntion('onlineState', record.onlineState);
      },
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.isEnable' }),
      dataIndex: 'enableState',
      hideInSearch: true,
      width: 100,
      render: (_, record) => {
        return changeFuntion('enableState', record.enableState);
      },
    },
    // {
    //   title: '故障状态',
    //   dataIndex: 'faultState',
    //   hideInSearch: true,
    //   width: 100,
    //   render: (_, record) => {
    //     return changeFuntion('faultState', record.faultState);
    //   },
    // },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.runningStatus' }),
      dataIndex: 'runState',
      hideInSearch: true,
      width: 100,
      render: (_, record) => {
        return changeFuntion('runState', record.runState);
      },
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.compressorCoolingStatus' }),
      dataIndex: 'comprState',
      hideInSearch: true,
      width: 140,
      render: (_, record) => {
        return changeFuntion('comprState', record.comprState);
      },
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.electricHeatingStatus' }),
      dataIndex: 'heatState',
      hideInSearch: true,
      width: 140,
      render: (_, record) => {
        return changeFuntion('heatState', record.heatState);
      },
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.selfCirculation' }),
      dataIndex: 'selfLoopState',
      hideInSearch: true,
      width: 80,
      render: (_, record) => {
        return changeFuntion('selfLoopState', record.selfLoopState);
      },
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.pumpSpeedPercentage' }),
      dataIndex: 'pumpSpeed',
      hideInSearch: true,
      width: 130,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.outdoorTemperature' }),
      dataIndex: 'outdoorTemp',
      hideInSearch: true,
      width: 110,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.inletWaterTemperature' }),
      dataIndex: 'inletWaterTemp',
      hideInSearch: true,
      width: 110,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.outletWaterTemperature' }),
      dataIndex: 'outletWaterTemp',
      hideInSearch: true,
      width: 110,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.inletWaterPressure' }),
      dataIndex: 'inletWaterPress',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.outletWaterPressure' }),
      dataIndex: 'outletWaterPress',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.fanOutputPercentage' }),
      dataIndex: 'fanOutput',
      hideInSearch: true,
      width: 130,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.faultLevel' }),
      dataIndex: 'faultLevel',
      hideInSearch: true,
      width: 100,
      render: (_, record) => {
        return changeFuntion('faultLevel', record.faultLevel);
      },
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.faultInfo' }),
      dataIndex: 'faultInfos',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.statusInfo' }),
      dataIndex: 'stateInfos',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.powerSwitch' }),
      dataIndex: 'onOff',
      hideInSearch: true,
      width: 100,
      render: (_, record) => {
        return changeFuntion('onOff', record.onOff);
      },
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.operationMode' }),
      dataIndex: 'setRunMode',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.setTemperature' }),
      dataIndex: 'setTemp',
      hideInSearch: true,
      width: 110,
    },
    {
      title: '控温回差模式设置',
      dataIndex: 'setDiffMode',
      hideInSearch: true,
      width: 145,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.coolingStartDifferential' }),
      dataIndex: 'coolStartDif',
      hideInSearch: true,
      width: 120,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.coolingStopDifferential' }),
      dataIndex: 'coolStopDif',
      hideInSearch: true,
      width: 120,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.heatingStartDifferential' }),
      dataIndex: 'heatStartDif',
      hideInSearch: true,
      width: 120,
    },
    {
      title: intl.formatMessage({ id: 'stationRealtimeMonitoring.heatingStopDifferential' }),
      dataIndex: 'heatStopDif',
      hideInSearch: true,
      width: 120,
    },
  ];
  const scroll = {
    x: 'max-content',
  };
  useEffect(() => {
    Promise.all([
      fetchDictInfo(DATABASE_TYPE_ENUM.WORKING_STATE_OF_DEHUMIDIFICATION, true),
      fetchDictInfo(DATABASE_TYPE_ENUM.ENABLE_DEVI, true),
      fetchDictInfo(DATABASE_TYPE_ENUM.LIQUID_COOLED_SWITCH, true),
      fetchDictInfo(DATABASE_TYPE_ENUM.ONLINE_STATUS, true),
      fetchDictInfo(DATABASE_TYPE_ENUM.LIQUID_COOLING_OPERATION_STATE, true),
      fetchDictInfo(DATABASE_TYPE_ENUM.LIQUID_COOLING_OPERATION_STATE_MODE, true),
      fetchDictInfo(DATABASE_TYPE_ENUM.LIQUID_COOLED_FAULT_LEVE, true),
    ]).then((res) => {
      Options_type1 = res[0];
      Options_type2 = res[1];
      Options_type3 = res[2];
      Options_online = res[3];
      Options_runState = res[4];
      Options_runMode = res[5];
      Options_faultLeve = res[6];
    });
  }, []);
  return (
    <div>
      <ProTable
        search={{
          labelWidth: 80,
        }}
        columns={columns}
        scroll={scroll}
        request={async (value) => {
          const params = {
            stationId: initialState?.stationId,
            current: value.current,
            pageSize: value.pageSize,
            deviceCode: deviceCode,
            collectStartTime: value.collectTime ? value.collectTime[0] : undefined,
            collectEndTime: value.collectTime ? value.collectTime[1] : undefined,
            reportStartTime: value.reportTime ? value.reportTime[0] : undefined,
            reportEndTime: value.reportTime ? value.reportTime[1] : undefined,
          };
          let reslut = await envDeviceControllerGetHistoryLiquidCoolData(params);
          return reslut;
          // return await envDeviceControllerGetHistoryAirCoolData(params);
        }}
        toolBarRender={() => []}
        customToolBarClass="global-tabs-custom-tools"
      ></ProTable>
    </div>
  );
};

export default HistoryData;
